﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;

namespace DiskScan_v02_FileToText
{
    class CountANDSize
    {
        public int _count;
        public long _size;

        public CountANDSize()
        {
            _count = 0;
            _size = 0;
        }

    }
    class Fileee
    {
        public string Name { get; set; }
        public long Size { get; set; }
    }
    class Simple
    {
        System.Collections.Specialized.StringCollection log = new System.Collections.Specialized.StringCollection();
        List<Fileee> fileee = new List<Fileee>();
        List<string> extension = new List<string>();
        String fileroot = @"D:\Documents\Вадик\TFS\helloworldnovfeb2015\vadim\DiskScan\DiskScan_v02\Test_Results\DiskScanResults.txt";

        CountANDSize Count(List<Fileee> fileee, string extension)
        {
            CountANDSize co = new CountANDSize();
            foreach (Fileee fi in fileee)
            {
                if (extension == fi.Name)
                {
                    co._count += 1;
                    co._size += fi.Size;
                }
            }
            return co;
        }
        bool IsConsist(List<string> Ext, string fExt)
        {
            int count = 0;
            foreach (string ext in Ext)
            {
                if (fExt == ext)
                    count += 1;
                else
                    count += 0;
            }
            if (count > 0)
                return true;
            else return false;
        }
        void GroupByEx1(FileInfo f)
        {
            fileee.Add(new Fileee() { Name = f.Extension, Size = f.Length });
            if (extension.Count == 0)
                extension.Add(f.Extension);

            if (IsConsist(extension, f.Extension))
            { }
            else
            {
                extension.Add(f.Extension);
            }
        }
        void Watch()
        {
            IEnumerable<IGrouping<string, long>> query = fileee.GroupBy(file => file.Name, file => file.Size);
            fileee.OrderBy(file => file.Size);
            using (System.IO.StreamWriter file = new System.IO.StreamWriter(fileroot, true))
            {
                foreach (IGrouping<string, long> fileGroup in query)
                {
                    file.Write("{0,-16}  ", fileGroup.Key);
                    file.WriteLine("Файлов:{0,5} шт,   Общий объем:{1,6} мб", Count(fileee, fileGroup.Key)._count, Count(fileee, fileGroup.Key)._size / 1024 / 1024);
                }
            }
        }
        void DisplayFiles()
        {
            DriveInfo[] drive = DriveInfo.GetDrives();

            foreach (DriveInfo d in drive)
            {
                DirectoryInfo dir = new DirectoryInfo(d.Name);
                if (d.IsReady)
                    CheckFiles(dir);
                DateTime dt = DateTime.Now;
                using (System.IO.StreamWriter file = new System.IO.StreamWriter(fileroot, true))
                {
                    file.WriteLine("");
                    file.WriteLine("{0}", dt.ToString("G"));
                    file.WriteLine("Сканирование диска {0}", d.Name);
                }
                Watch();
                fileee.Clear();
            }
        }
        void PrintExportFileRoot()
        {
            Console.WriteLine("{0}",fileroot);
        }
                void CheckFiles(DirectoryInfo dir)
        {
            FileInfo[] files = null;
            DirectoryInfo[] subDirs = null;

            try
            {
                files = dir.GetFiles("*.*");
            }

            catch (UnauthorizedAccessException e)
            {
                log.Add(e.Message);
            }

            if (files != null)
            {
                foreach (FileInfo f in files)
                {
                    GroupByEx1(f);
                }

                subDirs = dir.GetDirectories();

                foreach (DirectoryInfo dirf in subDirs)
                {
                    CheckFiles(dirf);
                }
            }
        }

        static void Main()
        {
            Simple sim = new Simple();
            Console.WriteLine("Производится експорт данных в файл:");
            sim.PrintExportFileRoot();
            sim.DisplayFiles();
            Console.WriteLine("Экспорт завершен. Для выхода нажмите любую клавишу...");
            Console.ReadLine();
        }
    }
}





